<script setup lang="ts">
import { RouterLink, RouterView } from 'vue-router'
import { ref, onMounted } from 'vue'
import { useThemeStore } from './stores/theme'
import { ElDropdown, ElDropdownMenu, ElDropdownItem, ElIcon } from 'element-plus'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'

// 初始化主题
const themeStore = useThemeStore()
onMounted(() => {
  themeStore.initTheme()
})

// 主题切换
const handleThemeChange = (command: 'light' | 'dark' | 'auto') => {
  themeStore.toggleTheme(command)
}
</script>

<template>
  <header>
    <div class="header-container">
      <div class="logo-section">
        <img alt="Vue logo" class="logo" src="@/assets/logo.svg" width="40" height="40" />
        <span class="logo-text">管理系统</span>
      </div>

      <div class="nav-section">
        <nav>
          <RouterLink to="/">首页</RouterLink>
          <RouterLink to="/about">关于</RouterLink>
        </nav>
      </div>

      <div class="actions-section">
        <!-- 主题切换 -->
        <el-dropdown @command="handleThemeChange">
          <div class="theme-toggle">
            <el-icon v-if="themeStore.themeMode === 'light'">
              <component :is="ElementPlusIconsVue.Sunny" />
            </el-icon>
            <el-icon v-else-if="themeStore.themeMode === 'dark'">
              <component :is="ElementPlusIconsVue.Moon" />
            </el-icon>
            <el-icon v-else>
              <component :is="ElementPlusIconsVue.Monitor" />
            </el-icon>
          </div>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item command="light" :disabled="themeStore.themeMode === 'light'">
                <el-icon><component :is="ElementPlusIconsVue.Sunny" /></el-icon> 浅色主题
              </el-dropdown-item>
              <el-dropdown-item command="dark" :disabled="themeStore.themeMode === 'dark'">
                <el-icon><component :is="ElementPlusIconsVue.Moon" /></el-icon> 深色主题
              </el-dropdown-item>
              <el-dropdown-item command="auto" :disabled="themeStore.themeMode === 'auto'">
                <el-icon><component :is="ElementPlusIconsVue.Monitor" /></el-icon> 跟随系统
              </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>

        <!-- 登录/注册 -->
        <div class="auth-links">
          <RouterLink to="/login" class="auth-link">登录</RouterLink>
          <RouterLink to="/register" class="auth-link register-link">注册</RouterLink>
        </div>
      </div>
    </div>
  </header>

  <RouterView />
</template>

<style scoped>
header {
  line-height: 1.5;
  max-height: 100vh;
  box-shadow: var(--el-box-shadow-light);
  background: var(--el-bg-color);
  position: sticky;
  top: 0;
  z-index: 1000;
}

.header-container {
  display: flex;
  align-items: center;
  justify-content: space-between;
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  height: 60px;
}

.logo-section {
  display: flex;
  align-items: center;
  gap: 10px;
}

.logo-text {
  font-size: 18px;
  font-weight: bold;
  color: var(--el-text-color-primary);
}

.nav-section nav {
  display: flex;
  gap: 20px;
}

.nav-section a {
  text-decoration: none;
  color: var(--el-text-color-primary);
  font-weight: 500;
  padding: 5px 10px;
  border-radius: 4px;
  transition: all 0.3s;
}

.nav-section a.router-link-exact-active {
  color: var(--el-color-primary);
  background-color: var(--el-color-primary-light-9);
}

.nav-section a:hover {
  color: var(--el-color-primary);
  background-color: var(--el-color-primary-light-9);
}

.actions-section {
  display: flex;
  align-items: center;
  gap: 20px;
}

.theme-toggle {
  cursor: pointer;
  padding: 5px;
  border-radius: 50%;
  transition: background-color 0.3s;
}

.theme-toggle:hover {
  background-color: var(--el-fill-color-light);
}

.auth-links {
  display: flex;
  gap: 10px;
}

.auth-link {
  text-decoration: none;
  color: var(--el-text-color-primary);
  padding: 6px 12px;
  border-radius: 4px;
  transition: all 0.3s;
  font-weight: 500;
}

.auth-link:hover {
  color: var(--el-color-primary);
  background-color: var(--el-color-primary-light-9);
}

.register-link {
  background-color: var(--el-color-primary);
  color: white;
}

.register-link:hover {
  background-color: var(--el-color-primary-dark-2);
  color: white;
}

@media (max-width: 768px) {
  .header-container {
    padding: 0 15px;
    height: 50px;
  }
  
  .logo-text {
    font-size: 16px;
  }
  
  .nav-section {
    display: none;
  }
}
</style>